package moo.stargalaxy_bukkit.network;

import com.alibaba.fastjson.JSONObject;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import moo.stargalaxy_bukkit.StarGalaxy_Bukkit;
import moo.stargalaxy_bukkit.api.GalaxyAPI;
import moo.stargalaxy_bukkit.config.Config;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class BukkitNetWork implements PluginMessageListener {
    @Override
    public void onPluginMessageReceived(String channel, Player player, byte[] message) {
        if (!channel.equalsIgnoreCase( "stargalaxy:message" ) ) {
            return;
        }
        //ByteArrayDataInput in = ByteStreams.newDataInput(message);
    }

    public static void sendCross(Player player, String server) {
        JSONObject serverInfo = GalaxyAPI.getServerInfo(server);
        Map o = serverInfo.getJSONObject("datas").getObject("servers", Map.class);
        ByteArrayDataOutput out = ByteStreams.newDataOutput();
        out.writeUTF("OtherSubchannel");
        out.writeUTF(server);
        out.writeUTF(o.get("连接地址") + ":" + o.get("分配端口"));
        player.sendPluginMessage(StarGalaxy_Bukkit.getPlugin(), "stargalaxy:message", out.toByteArray());
    }

    public static void sendHub(Player player, String server) {
        ByteArrayDataOutput out = ByteStreams.newDataOutput();
        out.writeUTF("OtherSubchannel");
        out.writeUTF("hub");
        player.sendPluginMessage(StarGalaxy_Bukkit.getPlugin(), "stargalaxy:message", out.toByteArray());
    }

    public static void sendCustomData(Player player, String name, String...data) {
        ByteArrayDataOutput out = ByteStreams.newDataOutput();
        out.writeUTF(name);
        for (String datum : data) {
            out.writeUTF(datum);
        }
        // 蹦极必须要求玩家在线才能执行
        player.sendPluginMessage(StarGalaxy_Bukkit.getPlugin(), "stargalaxy:message", out.toByteArray());
    }
}
